//和用户相关的状态管理
import { createSlice } from '@reduxjs/toolkit'
import { getToken, setToken as _setToken , removeToken} from '@/utils'
import { loginAPI, getProfileAPI } from '@/api/user'

const userStore = createSlice({
    name: 'user',
    initialState:{
        token:getToken() || '',
        userInfo: {}
    },
    reducers:{
        //同步修改方法
        setToken(state, action){
            state.token = action.payload
            //localStorage也存一份
            _setToken(action.payload)
        },
        setUserInfo(state, action){
            state.userInfo = action.payload
        },
        clearUserInfo(state){
            state.token = ''
            state.userInfo = {}
            //清除本地存储的token
            removeToken()
        }
    }
})

//解构出actionCreator
const { setToken, setUserInfo, clearUserInfo } = userStore.actions

//获取reducer函数   
const userReducer = userStore.reducer

//异步方法 完成登录获取token
const fetchLogin = (loginForm) => {
    return async (dispatch) => {
        //1.发送异步请求
        const res = await loginAPI(loginForm)
        //2.提交同步action进行token的存入
        dispatch(setToken(res.data.token))
    }
}

const fetchUserInfo = () => {
    return async (dispatch) => {
        //1.发送异步请求
        const res = await getProfileAPI()
        //2.提交同步action
        dispatch(setUserInfo(res.data))
    }
}

export { fetchLogin, fetchUserInfo, clearUserInfo }
export default userReducer